var ec2_Createlb = {
    ec2ui_session : null,
    retVal : null,
    reginstanceid : new Array(),
    launch : function() {
       // var Idx = 0;
        this.retVal.LoadBalancerName = document.getElementById("ec2ui.createlb.Name").value.trim();
	this.retVal.Protocol = document.getElementById("ec2ui.createlb.Protocol").value.trim();
        this.retVal.elbport = document.getElementById("ec2ui.createlb.elbport").value.trim();
	//this.retVal.azone = document.getElementById("ec2ui.createlb.azone").value.trim();
	this.retVal.instanceport = document.getElementById("ec2ui.createlb.instanceport").value.trim();
	this.retVal.pingprotocol = document.getElementById("ec2ui.createlb.pingprotocol").value.trim();
	this.retVal.pingport = document.getElementById("ec2ui.createlb.pingport").value.trim();
	this.retVal.pingpath = document.getElementById("ec2ui.createlb.pingpath").value.trim();
        this.retVal.Interval = document.getElementById("ec2ui.createlb.Interval").value.trim();
	this.retVal.Timeout = document.getElementById("ec2ui.createlb.timeout").value.trim();
	this.retVal.HealthyThreshold = document.getElementById("ec2ui.createlb.HThreshold").value.trim();
	this.retVal.UnhealthyThreshold = document.getElementById("ec2ui.createlb.uThreshold").value.trim();
	//this.retVal.Instances = document.getElementById("celliid"+Idx).value.trim();
	
	var listBox = document.getElementById('theList');
	var idx = 0;
	var nRowCount = listBox.getRowCount();
	
	this.retVal.Instances = "";
	for(idx=0;idx < nRowCount;idx++)
	{
	    var cellID = "cellcheck"+idx;	    
	    var cell = document.getElementById(cellID);
	    
	    if(cell.hasAttribute('checked','true'))
	    {
		var cellinstance = "celliid"+idx;
		
		var instance = document.getElementById(cellinstance);
		//var azones = document.getElementById(azoneid);
		var reginstanceid = instance.getAttribute('label');
		//var regazone = azones.getAttribute('label');
		//alert(regazone);
		this.retVal.Instances =  this.retVal.Instances + reginstanceid +",";;
		//this.retVal.azone =  this.retVal.Instances +","+ regazone ;
		//this.retVal.azone = this.retVal.azone + regzone+",";
	    }
	}
	
	
	
	
	this.retVal.ok = true;
        return true;
    },
    
    init : function() {
        this.ec2ui_session = window.arguments[0];
        this.retVal = window.arguments[1];
	
	var theList = document.getElementById('theList');
	//alert("Here");
	var Instanceid = this.ec2ui_session.model.getInstances();
	var Idx = 0;
	for (var i in Instanceid) {
	var row = document.createElement('listitem');
	var cell1 = document.createElement('listcell');
	var cell2 = document.createElement('listcell');
	var cell3 = document.createElement('listcell');
        var cell4 = document.createElement('listcell');
	    
	    
	    var cellID = "cellcheck"+Idx;
	    cell1.setAttribute('id',cellID);
	    
	    var cellinstance = "celliid"+Idx;
	    //this.retVal.Instances = document.getElementById("celliid"+Idx).value.trim();
	    cell2.setAttribute('id',cellinstance);
	    
	    var azoneid = "cellizone"+Idx;
	    cell4.setAttribute('id',azoneid);
	    
	    cell1.setAttribute('type', 'checkbox');
	    //cell1.setAttribute('checked', 'true');
	    //cell1.setAttribute('onclick', 'ec2_Createlb.test(event)');
	    row.appendChild(cell1);
	    
	    cell2.setAttribute('label', Instanceid[i].id);
	    row.appendChild(cell2);

           cell3.setAttribute('label',  Instanceid[i].state);
           row.appendChild(cell3);
	    
	    cell4.setAttribute('label',  Instanceid[i].placement.availabilityZone);
	    row.appendChild(cell4);
	     
	    var rowID = "row"+Idx;
	    row.setAttribute('id',rowID);
	    theList.appendChild(row);
	    Idx = Idx +1;
	}
  },
  
  reg_unreg_instances : function(){
     //alert("row");
      var listBox = document.getElementById('theList');
      var selectedItem = listBox.currentIndex;
      if (selectedItem == -1) return null;
     // alert(selectedItem);
     // var row = listBox.getSelectedItem( selectedItem );
      var rowID = "row"+selectedItem;
      var row = document.getElementById(rowID);
     // var cell = row.getItemAtIndex(0);
     // alert(row);
      var cellID = "cellcheck"+selectedItem;
      var cellinstance = "celliid"+selectedItem;
      //cell1.setAttribute('id',cellID);
      var cell = document.getElementById(cellID);
      var instance = document.getElementById(cellinstance);
      var attribute = cell.getAttribute('type');
      //alert(attribute);
      
      var reginstanceid = instance.getAttribute('label');
      //alert("1"+ cell.checked);
      //cell.setAttribute('checked',!cell.checked);
      //alert("2"+ cell.checked);
      //alert(cell.hasAttribute('checked','true'));
      if(cell.hasAttribute('checked','true'))
      {
      cell.setAttribute('checked','false');
      cell.removeAttribute('checked');
     //alert("1"+ cell.checked);
      }
      else
      {
      cell.setAttribute('checked','true');
      //alert("0"+ cell.checked);
      }
    },
    
    instancedetails : function(){
	var instanceid = this.reginstanceid;
	alert(instanceid);
    }
}